package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import entity.ListPageEntity;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/18.
 */
public class ListPageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE00 = 0;
    private static final int TYPE01 = 1;
    private static final int TYPE02 = 2;
    private Context context;
    private ImageLoader imageLoader;
    private ListPageEntity entity;

    public ListPageAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(ListPageEntity data) {
        entity = data;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case TYPE00:
                return new ViewHolder00(inflater.inflate(R.layout.list_page_itemz, parent, false));
            case TYPE01:
                return new ViewHolder01(inflater.inflate(R.layout.list_page_itemo, parent, false));
            case TYPE02:
                return new ViewHolder02(inflater.inflate(R.layout.list_page_itemt, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (entity != null) {
            if (holder instanceof ViewHolder00) {
                if (entity.getFocusImages().getList() != null && entity.getFocusImages().getList().size() > 0) {
                    String url = entity.getFocusImages().getList().get(0).getPic();
                    ((ViewHolder00) holder).listItemFocusImg.setImageUrl(url, imageLoader);
                }
            }
            if (holder instanceof ViewHolder01) {
            }
            if (holder instanceof ViewHolder02) {
                ((ViewHolder02) holder).ListPageTitle.setText(entity.getDatas().get(0).getTitle());
            }
        }
    }

    @Override
    public int getItemCount() {

        return 3;
    }

    @Override
    public int getItemViewType(int position) {
if (entity != null){


            if (entity.getFocusImages().getTitle().equals("焦点图")) {
                return TYPE00;
            }
        return TYPE02;
}
        return TYPE02;


    }


    public class ViewHolder00 extends RecyclerView.ViewHolder {
        private NetworkImageView listItemFocusImg;

        public ViewHolder00(View itemView) {
            super(itemView);
            listItemFocusImg = (NetworkImageView) itemView.findViewById(R.id.listItemFocusImg);
        }
    }

    public class ViewHolder01 extends RecyclerView.ViewHolder {
        private TextView ListPageTitle;

        public ViewHolder01(View itemView) {
            super(itemView);
            ListPageTitle = (TextView) itemView.findViewById(R.id.ListPageTitle);
        }
    }

    public class ViewHolder02 extends RecyclerView.ViewHolder {
        private TextView ListPageTitle, listItemTitleo, listItemTitlet, listItemTitleth,
                listItemContento, listItemContentt, listItemContentth;
        private NetworkImageView jmListPageItemImgo, jmListPageItemImgt, jmListPageItemImgth;

        public ViewHolder02(View itemView) {
            super(itemView);
            jmListPageItemImgo = (NetworkImageView) itemView.findViewById(R.id.jmListPageItemImgo);
            jmListPageItemImgt = (NetworkImageView) itemView.findViewById(R.id.jmListPageItemImgt);
            jmListPageItemImgth = (NetworkImageView) itemView.findViewById(R.id.jmListPageItemImgth);
            ListPageTitle = (TextView) itemView.findViewById(R.id.ListPageTitle);
            listItemTitleo = (TextView) itemView.findViewById(R.id.listItemTitleo);
            listItemTitlet = (TextView) itemView.findViewById(R.id.listItemTitlet);
            listItemTitleth = (TextView) itemView.findViewById(R.id.listItemTitleth);

        }
    }
}
